package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.dto.TestBean;
import com.example.demo.util.ConnectionUtil;
import com.example.demo.util.ExcelUtils;
import com.example.demo.util.RequestDto;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
@Slf4j
public class TestController extends TheadLocalController{

    private final  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

    private static final   String  guaranteePath = "%s@/upfile/10002/%s/guarantee_%s.pdf";

    private  static  final  String contractPath = "%s@/upfile/10003/%s/contract_%s.pdf";

    @RequestMapping("test")
    @ResponseBody
    public String test(@RequestBody String str) throws Exception {
        String[] list = str.split(",");
        Connection connection = ConnectionUtil.getConnection();
        String sql = "SELECT * FROM cst_loan WHERE id_no  = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        List<TestBean> testBeans = new ArrayList<>();
        for (String idCard : list) {
            preparedStatement.setString(1,idCard);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                TestBean testBean = new TestBean();
                testBean.setLoanNo(resultSet.getString("loan_no"));
                testBean.setLoanTime(simpleDateFormat.format(resultSet.getDate("loan_time")));
                System.out.println(String.format(guaranteePath,idCard,testBean.getLoanTime(), testBean.getLoanNo()) + "@" + String.format(contractPath,idCard, testBean.getLoanTime(), testBean.getLoanNo()));
                testBeans.add(testBean);
            }
        }
        return JSON.toJSONString(testBeans);
    }

    @RequestMapping("cacheData")
    @ResponseBody
    @Cacheable(value = "cacheData",key = "'cacheData123'")
    public String cacheData () throws Exception {
        return  "123";
//        Connection connection = ConnectionUtil.getConnection();
//        String sql = "SELECT * FROM cst_loan LIMIT 1";
//        Statement statement = connection.createStatement();
//        ResultSet resultSet = statement.executeQuery(sql);
//        StringBuilder resp = new StringBuilder("haha");
//        while (resultSet.next()) {
//            String loanNo = resultSet.getString("loan_no");
//            resp.append(loanNo);
//        }
//        return resp.toString();
    }

    @RequestMapping("test3")
    @ResponseBody
    public String test3(ServletRequest request) throws  Exception{
        StringBuilder body = new StringBuilder();
        BufferedReader reader = request.getReader();
        reader.lines().forEach(body::append);
        return  body.toString();
    }

    @RequestMapping("test2")
    @ResponseBody
    public String test2() {
        RequestDto requestDto = getParam();
        return  requestDto.getData();
    }

    @RequestMapping("export")
    @ResponseBody
    @ApiOperation(value = "导出文件",notes = "文件导出")
    public void export(HttpServletResponse response) throws Exception{
        List<TestBean> list = new ArrayList<>();
        for (int i=0;i<10;i++ ) {
            TestBean testBean = new TestBean();
            testBean.setLoanNo("1234loan_" + i);
            testBean.setLoanTime("20200501");
            list.add(testBean);
        }
        String fileName = "测试";
        String sheetName = "模板";
        ExcelUtils.exportExcel(response,fileName,list,TestBean.class,sheetName);
    }

    public static void main(String[] args) throws Exception {

//        String str = "{\"loanNo\":\"123\",\"loanTime\":\"2022-05-01\"}";
//
//        TestBean testBean = JSON.parseObject(str, TestBean.class);
//
//        System.out.println(JSON.toJSONString(testBean));


//        List<TestBean> testBeans = new ArrayList<>();
        TestBean testBean = new TestBean();
        testBean.setLoanNo("123");
        testBean.setLoanTime("2022-05-01");
        Map<String, String> map1 = JSONObject.parseObject(JSONObject.toJSONString(testBean), new com.alibaba.fastjson.TypeReference<Map<String, String>>() {});
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> map = objectMapper.readValue(objectMapper.writeValueAsBytes(testBean), new TypeReference<Map<String, String>>() {});
        System.out.println(JSON.toJSONString(map));
//        testBeans.add(testBean);
//        TestBean testBean2= new TestBean();
//        testBean2.setLoanNo("456");
//        testBean2.setLoanTime("2022-06-01");
//        testBeans.add(testBean2);
//        TestBean testBean3= new TestBean();
//        testBean3.setLoanNo("456");
//        testBean3.setLoanTime("2022-07-01");
//        testBeans.add(testBean3);
//
//        Map<String, Long> collect = testBeans.stream().collect(Collectors.groupingBy(TestBean::getLoanNo, Collectors.counting()));
//        System.out.println(JSON.toJSONString(collect));
//
//        testBeans= testBeans.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TestBean::getLoanNo))), ArrayList::new));
//        System.out.println(JSON.toJSONString(testBeans));

    }

    }
